package com.alex.seo.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.core.annotation.Order;

import com.alex.seo.service.logging.LoggingService;

@Aspect
@Order(1)
class ExceptionHandlerAspect extends GenericPointcuts {
	
	private static final Logger LOGGER = LoggerFactory.getLogger(ExceptionHandlerAspect.class); 
	
	private LoggingService loggingService;
	
	@Required
	public void setLoggingService(LoggingService loggingService) {
		this.loggingService = loggingService;
	}

	@Around(value = "service() && !logging() || job()")
	public Object handleError(ProceedingJoinPoint joinPoint) throws Throwable {
		try {
			return joinPoint.proceed();
		} catch (Throwable t) {
			LOGGER.error(t.getMessage(), t);
			loggingService.logError(t);
			throw t;
		}
	}

}
